package com.luoye.lj.util.converter;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: Dave Liu
 * Date: 15-10-20
 * Time: 上午11:27
 * 对象转换,将属性名相同的字段进行转换
 */
public class DataConverter<S,T> {
    S source;
    T target;
    public DataConverter(S source, T target){
        this.source=source;
        this.target=target;
    }
    public void convert(){
        Field[] sFields=source.getClass().getDeclaredFields();
        for(Field sField:sFields){
            String sFieldName=sField.getName();
            String sFieldType=sField.getType().getSimpleName();
            Field tField=null;
            try {
                tField=target.getClass().getDeclaredField(sFieldName);
                String tFieldType=tField.getType().getSimpleName();
                sField.setAccessible(true);
                tField.setAccessible(true);
                if(sFieldType.equals(tFieldType)){
                    tField.set(target,sField.get(source));
                }else{//不同字段类型之间的转换
                    if ("int".equals(tFieldType)) {
                        try {
                           int value= Integer.parseInt(sField.get(source) + "");
                           tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    } else if ("long".equals(tFieldType)) {
                        try {
                            long value= Long.parseLong(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    } else if ("float".equals(tFieldType)) {
                        try {
                            float value= Float.parseFloat(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    } else if ("double".equals(tFieldType)) {
                        try {
                            double value= Double.parseDouble(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    } else if ("char".equals(tFieldType)) {
                        try {
                            char value=(char) Integer.parseInt(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    } else if ("byte".equals(tFieldType)) {
                        try {
                            byte value= Byte.parseByte(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    } else if ("short".equals(tFieldType)) {
                        try {
                            short value= Short.parseShort(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
                    }
					else if ("boolean".equals(tFieldType)) {
                        try {
                            boolean value= Boolean.parseBoolean(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (NumberFormatException ex){
                            ex.printStackTrace();;
                        }
					}
                    else if ("Date".equals(tFieldType)) {
                        SimpleDateFormat sdtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        try {
                            Date value=sdtf.parse(sField.get(source) + "");
                            tField.set(target,value);
                        }catch (ParseException ex){
                            ex.printStackTrace();;
                        }
                    }else if ("String".equals(tFieldType)) {
                        String value=sField.get(source) + "";
                        tField.set(target,value);
                    }
                }
            }catch (NoSuchFieldException ex){
               //ex.printStackTrace();
            }catch (IllegalAccessException ex){
                //ex.printStackTrace();
            }
        }
    }
}
